package com.llw.notify.setting;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.llw.notify.databinding.ActivityAddBlackValueBinding;

public class ManageBlackValueActivity extends AppCompatActivity {
    private final String TAG = getClass().getSimpleName();
    private ActivityAddBlackValueBinding binding;

    public final static String WORD = "word";
    public final static String APP = "app";
    private String type;
    private String[] oldValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAddBlackValueBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Intent intent = getIntent();
        type = intent.getStringExtra("type");

        initUIContent();
        loadOldValue();
        makeInputNonEditable();
        binding.edit.setOnClickListener((view) -> edit());
        binding.update.setOnClickListener((view) -> update());
        binding.reset.setOnClickListener((view) -> {
            loadOldValue();
            shortToast("已重置");
        });
    }

    private void initUIContent() {
        switch (type) {
            case ManageBlackValueActivity.APP:
                setTitle("屏蔽应用管理");
                binding.title.setText("现有屏蔽应用");
                oldValues = SettingsManager.getBlackApps();
                break;
            case ManageBlackValueActivity.WORD:
                setTitle("屏蔽词管理");
                binding.title.setText("现有屏蔽词");
                oldValues = SettingsManager.getBlackWords();
                break;
            default:
                binding.title.setText("未知类型");
                shortToast("未知类型，直接返回");
                finish();
                break;
        }
    }

    private void loadOldValue() {
        StringBuilder stringBuilder = new StringBuilder();
        for (String oldValue : oldValues) {
            stringBuilder.append(oldValue).append("\n");
        }
        binding.input.setText(stringBuilder.toString());
    }

    private void edit() {
        switch (type) {
            case ManageBlackValueActivity.APP:
                binding.title.setText("编辑屏蔽应用");
                oldValues = SettingsManager.getBlackApps();
                break;
            case ManageBlackValueActivity.WORD:
                binding.title.setText("编辑屏蔽词");
                oldValues = SettingsManager.getBlackWords();
                break;
            default:
                binding.title.setText("未知类型");
                return;
        }
        makeInputEditable();
        shortToast("开始编辑");
    }

    private void makeInputEditable() {
        binding.input.setFocusable(true);
        binding.input.setFocusableInTouchMode(true);
        binding.input.setLongClickable(true);
        binding.input.requestFocus();
    }

    private void makeInputNonEditable() {
        binding.input.setFocusable(false);
        binding.input.setFocusableInTouchMode(false);
        binding.input.setLongClickable(false);
    }

    private void shortToast(final String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    private void update() {
        final String newValue = binding.input.getText().toString();
        if (newValue.isEmpty()) {
            shortToast("您选择了清空现有内容");
            return;
        }
        switch (type) {
            case ManageBlackValueActivity.WORD:
                SettingsManager.updateBlackWords(newValue);
                shortToast("修改屏蔽词成功");
                break;
            case ManageBlackValueActivity.APP:
                SettingsManager.updateBlackApps(newValue);
                shortToast("修改屏蔽应用成功");
                break;
            default:
                shortToast("未知屏蔽类型");
                break;
        }
    }
}